<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="130px">
      <el-form-item label="反馈信息名称" prop="feedbackTitle">
        <el-input v-model="queryParams.feedbackTitle" placeholder="请输入反馈信息名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="反馈信息联系方式" prop="phone">
        <el-input v-model="queryParams.phone" placeholder="请输入反馈信息联系方式" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['customer:feedback:add']">新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['customer:feedback:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['customer:feedback:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['customer:feedback:export']">导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="feedbackList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="反馈信息名称" align="center" prop="feedbackTitle" />
      <el-table-column label="反馈信息内容" align="center" prop="content" />
      <el-table-column label="反馈信息联系方式" align="center" prop="phone" />
      <el-table-column label="会员反馈审核状态" align="center" prop="statusName" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['customer:feedback:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['customer:feedback:remove']">删除</el-button>
          <el-button size="mini" type="text" icon="el-icon-check" @click="handleAudit(scope.row)"
            v-hasPermi="['customer:feedback:edit']">审核</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改会员反馈信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="反馈信息名称" prop="feedbackTitle">
          <el-input v-model="form.feedbackTitle" placeholder="请输入反馈信息名称" />
        </el-form-item>
        <el-form-item label="反馈信息内容">
          <el-input v-model="form.content" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="联系方式" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入反馈信息联系方式" />
        </el-form-item>
        <el-form-item label="审核状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择审核状态">
            <el-option v-for="dict in dict.type.feedback_status" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-divider content-position="center">会员反馈信息图片信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <!-- <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddCustomerFeedbackImg">添加</el-button>
          </el-col> -->
          <el-col :span="1.5">
            <el-upload class="upload-demo" ref="upload" :action="uploadImgUrl" :on-preview="handlePreview"
              :on-remove="handleRemove" :on-success="handleSuccess" :file-list="fileList" :auto-upload="false">
              <el-button slot="trigger" size="mini" type="primary">选取文件</el-button>
              <el-button style="margin-left: 10px;" size="mini" type="success" @click="submitUpload">上传到服务器</el-button>
              <el-button type="danger" icon="el-icon-delete" size="mini"
                @click="handleDeleteCustomerFeedbackImg">删除</el-button>
              <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
            </el-upload>
          </el-col>
        </el-row>
        <el-table :data="customerFeedbackImgList" :row-class-name="rowCustomerFeedbackImgIndex"
          @selection-change="handleCustomerFeedbackImgSelectionChange" ref="customerFeedbackImg">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50" />
          <el-table-column label="图片连接地址" prop="imgUrl" width="150">
            <template slot-scope="scope">
              <img style="width: 100px; height: 100px;" :src="`${baseUrl}${scope.row.imgUrl}`">
              <!-- <el-input v-model="scope.row.imgUrl" placeholder="请输入图片连接地址" /> -->
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFeedback, getFeedback, delFeedback, addFeedback, updateFeedback, auditFeedback } from "@/api/customer/feedback";

export default {
  name: "Feedback",
  dicts: ['feedback_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 上传文件列表
      fileList: [],
      baseUrl: process.env.VUE_APP_BASE_API,
      uploadImgUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址
      // 子表选中数据
      checkedCustomerFeedbackImg: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 会员反馈信息表格数据
      feedbackList: [],
      // 会员反馈信息图片表格数据
      customerFeedbackImgList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerId: null,
        feedbackTitle: null,
        content: null,
        phone: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询会员反馈信息列表 */
    getList() {
      this.loading = true;
      listFeedback(this.queryParams).then(response => {
        this.feedbackList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    submitUpload() {
      console.log(this.fileList);
      this.$refs.upload.submit();
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
    handleSuccess(response, file, fileList) {
      this.fileList = fileList;
      this.customerFeedbackImgList = [];
      for (let i = 0; i < fileList.length; i++) {
        var img = fileList[i];
        let obj = {};
        obj.imgUrl = img.response.fileName;
        this.customerFeedbackImgList.push(obj);
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        customerFeedbackId: null,
        customerId: null,
        feedbackTitle: null,
        content: null,
        phone: null,
        status: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.customerFeedbackImgList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.customerFeedbackId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加会员反馈信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const customerFeedbackId = row.customerFeedbackId || this.ids
      getFeedback(customerFeedbackId).then(response => {
        this.form = response.data;
        this.customerFeedbackImgList = response.data.customerFeedbackImgList;
        this.open = true;
        this.title = "修改会员反馈信息";
      });
    },
    handleAudit(row) {
      var requestObj = {};
      requestObj.feedbackId = row.customerFeedbackId;
      auditFeedback(requestObj).then(response => {
        this.$modal.msgSuccess(response.msg);
        this.getList();
      });
    },
    /** 提交按钮 */
    submitForm() {
      if (this.form.feedbackTitle == null || this.form.feedbackTitle == "") {
        this.$modal.msgError("请填写反馈内容的标题");
        return;
      }
      if (this.form.content == null || this.form.content == "") {
        this.$modal.msgError("请填写反馈内容");
        return;
      }
      if (this.form.phone == null || this.form.phone == "") {
        this.$modal.msgError("请填写联系方式");
        return;
      }
      if (this.form.status == null || this.form.status == "") {
        this.$modal.msgError("请选择审核状态");
        return;
      }
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.customerFeedbackImgList = this.customerFeedbackImgList;
          if (this.form.customerFeedbackId != null) {
            updateFeedback(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFeedback(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const customerFeedbackIds = row.customerFeedbackId || this.ids;
      this.$modal.confirm('是否确认删除会员反馈信息编号为"' + customerFeedbackIds + '"的数据项？').then(function () {
        return delFeedback(customerFeedbackIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 会员反馈信息图片序号 */
    rowCustomerFeedbackImgIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 会员反馈信息图片添加按钮操作 */
    handleAddCustomerFeedbackImg() {
      let obj = {};
      obj.imgUrl = "";
      obj.remark = "";
      this.customerFeedbackImgList.push(obj);
    },
    /** 会员反馈信息图片删除按钮操作 */
    handleDeleteCustomerFeedbackImg() {
      if (this.checkedCustomerFeedbackImg.length == 0) {
        this.$modal.msgError("请先选择要删除的会员反馈信息图片数据");
      } else {
        const customerFeedbackImgList = this.customerFeedbackImgList;
        const checkedCustomerFeedbackImg = this.checkedCustomerFeedbackImg;
        this.customerFeedbackImgList = customerFeedbackImgList.filter(function (item) {
          return checkedCustomerFeedbackImg.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleCustomerFeedbackImgSelectionChange(selection) {
      this.checkedCustomerFeedbackImg = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('customer/feedback/export', {
        ...this.queryParams
      }, `feedback_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
